package com.java110.mall.goods.bmo.assetInventory.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.asset.AssetInventoryDto;
import com.java110.dto.asset.AssetInventoryProductDto;
import com.java110.mall.goods.bmo.assetInventory.IUpdateAssetInventoryBMO;
import com.java110.intf.goods.IAssetInventoryInnerServiceSMO;
import com.java110.intf.goods.IAssetInventoryProductInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.po.assetInventory.AssetInventoryPo;
import com.java110.po.assetInventory.AssetInventoryProductPo;
import com.java110.po.product.ProductSpecValuePo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("updateAssetInventoryBMOImpl")
public class UpdateAssetInventoryBMOImpl implements IUpdateAssetInventoryBMO {

    @Autowired
    private IAssetInventoryInnerServiceSMO assetInventoryInnerServiceSMOImpl;

    @Autowired
    private IAssetInventoryProductInnerServiceSMO assetInventoryProductInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    /**
     * @param assetInventoryPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> update(AssetInventoryPo assetInventoryPo, JSONArray inoutOrderProducts) {

        AssetInventoryProductPo assetInventoryProductPo = new AssetInventoryProductPo();
        assetInventoryProductPo.setAiId(assetInventoryPo.getAiId());
        assetInventoryProductPo.setShopId(assetInventoryPo.getShopId());
        int flag = assetInventoryProductInnerServiceSMOImpl.deleteAssetInventoryProduct(assetInventoryProductPo);
        if (flag < 1) {
            throw new IllegalArgumentException("删除上次失败");
        }
        List<AssetInventoryProductPo> assetInventoryProductPos = new ArrayList<>();
        for (int productIndex = 0; productIndex < inoutOrderProducts.size(); productIndex++) {
            dealSaveAssetInventoryProduct(inoutOrderProducts.getJSONObject(productIndex), assetInventoryPo, assetInventoryProductPos);
        }

        flag = assetInventoryProductInnerServiceSMOImpl.saveAssetInventoryProducts(assetInventoryProductPos);

        if (flag < 1) {
            throw new IllegalArgumentException("保存失败");
        }


        flag = assetInventoryInnerServiceSMOImpl.updateAssetInventory(assetInventoryPo);

        if (flag < 1) {
            throw new IllegalArgumentException("修改失败");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
    }

    /**
     * 入库
     *
     * @param assetInventoryPo
     * @param products
     * @return
     */
    @Override
    @Java110Transactional
    public ResponseEntity<String> inStock(AssetInventoryPo assetInventoryPo, JSONArray products) {

        for (int productIndex = 0; productIndex < products.size(); productIndex++) {
            doInStockProduct(products.getJSONObject(productIndex), assetInventoryPo);
        }
        AssetInventoryProductDto assetInventoryProductDto = new AssetInventoryProductDto();
        assetInventoryProductDto.setAiId(assetInventoryPo.getAiId());
        assetInventoryProductDto.setShopId(assetInventoryPo.getShopId());
        List<AssetInventoryProductDto> assetInventoryProductDtos
                = assetInventoryProductInnerServiceSMOImpl.queryAssetInventoryProducts(assetInventoryProductDto);

        if (assetInventoryProductDtos == null || assetInventoryProductDtos.size() < 1) {
            return ResultVo.success();
        }
        boolean hasFinish = true;
        for (AssetInventoryProductDto tAssetInventoryProductDto : assetInventoryProductDtos) {
            if (!tAssetInventoryProductDto.getState().equals(AssetInventoryDto.STATE_IN_STORE)) {
                hasFinish = false;
                break;
            }
        }
        AssetInventoryPo tAssetInventoryPo = new AssetInventoryPo();
        tAssetInventoryPo.setAiId(assetInventoryPo.getAiId());
        tAssetInventoryPo.setShopId(assetInventoryPo.getShopId());
        if (hasFinish) {
            tAssetInventoryPo.setState(AssetInventoryDto.STATE_IN_STORE);
        }
        tAssetInventoryPo.setState(AssetInventoryDto.STATE_FINISH);
        int flag = assetInventoryInnerServiceSMOImpl.updateAssetInventory(tAssetInventoryPo);
        if (flag < 1) {
            throw new IllegalArgumentException("入库失败");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "成功");
    }

    /**
     * 单个盘点产品处理
     *
     * @param product
     * @param assetInventoryPo
     */
    private void doInStockProduct(JSONObject product, AssetInventoryPo assetInventoryPo) {
        AssetInventoryProductDto assetInventoryProductDto = new AssetInventoryProductDto();
        assetInventoryProductDto.setDetailId(product.getString("detailId"));
        assetInventoryProductDto.setShopId(product.getString("shopId"));
        List<AssetInventoryProductDto> assetInventoryProductDtos
                = assetInventoryProductInnerServiceSMOImpl.queryAssetInventoryProducts(assetInventoryProductDto);

        Assert.listOnlyOne(assetInventoryProductDtos, "未找到入库商品");

        ProductSpecValuePo productSpecValuePo = new ProductSpecValuePo();
        productSpecValuePo.setShopId(assetInventoryProductDtos.get(0).getShopId());
        productSpecValuePo.setStock(assetInventoryProductDtos.get(0).getInvQuantity());
        productSpecValuePo.setProductId(assetInventoryProductDtos.get(0).getProductId());
        productSpecValuePo.setValueId(assetInventoryProductDtos.get(0).getValueId());
        productSpecValuePo.setAttrition(assetInventoryProductDtos.get(0).getAttrition());
        productSpecValuePo.setWillExpire(assetInventoryProductDtos.get(0).getWillExpire());
        productSpecValuePo.setStatusCd("0");
        productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);

        AssetInventoryProductPo assetInventoryProductPo = new AssetInventoryProductPo();
        assetInventoryProductPo.setDetailId(assetInventoryProductDtos.get(0).getDetailId());
        assetInventoryProductPo.setShopId(assetInventoryProductDtos.get(0).getShopId());
        assetInventoryProductPo.setStatusCd("0");
        assetInventoryProductPo.setState(AssetInventoryDto.STATE_IN_STORE);

        assetInventoryProductInnerServiceSMOImpl.updateAssetInventoryProduct(assetInventoryProductPo);
    }

    /**
     * 保存盘点产品
     *
     * @param jsonObject
     * @param assetInventoryPo
     */
    private void dealSaveAssetInventoryProduct(JSONObject jsonObject, AssetInventoryPo assetInventoryPo, List<AssetInventoryProductPo> assetInventoryProductPos) {
        AssetInventoryProductPo assetInventoryProductPo = BeanConvertUtil.covertBean(jsonObject, AssetInventoryProductPo.class);
        assetInventoryProductPo.setAiId(assetInventoryPo.getAiId());
        assetInventoryProductPo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId));
        assetInventoryProductPo.setState(AssetInventoryDto.STATE_DOING);
        assetInventoryProductPos.add(assetInventoryProductPo);
    }

}
